﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TJDevHouse.Starcraft.Game;

using TJDevHouse.Starcraft.AIModuleConnector.ImportExport;

namespace TJDevHouse.Starcraft.MapAnalyser
{
	abstract public class Analyser
	{
		/// <summary>
		/// The analysis controller this analyser belongs to
		/// </summary>
		public MapAnalysisController AnalysisController { get; private set; }

		/// <summary>
		/// The map used in the analysis
		/// </summary>
		public Map Map { get; private set; }

		/// <summary>
		/// Whether the analysis on this has been run
		/// </summary>
		public bool Analysed { get; protected set; }

		/// <summary>
		/// Whether or not this analyser has its result data saved for the current
		/// map.
		/// </summary>
		public bool AnalysisResultSaved
		{
			get
			{
				return MapDataStorageController.MapDataIsSaved(Map, AnalyserRefName);
			}
		}

		/// <summary>
		/// The unique reference name for this analyser - primarily used for
		/// the name of analysis result files
		/// </summary>
		public string AnalyserRefName
		{
			get
			{
				return GetAnalyserRefName();
			}
		}

		public Analyser(Map map, MapAnalysisController analysisController)
		{
			Map = map;
			Analysed = false;
			AnalysisController = analysisController;
		}

		/// <summary>
		/// Get the unique reference name for this analyser
		/// </summary>
		/// <returns></returns>
		abstract public string GetAnalyserRefName();

		/// <summary>
		/// Perform this analyser's analysis
		/// </summary>
		abstract public void DoAnalysis();

		/// <summary>
		/// Perform this analyset's analysis and optionally save the result
		/// </summary>
		/// <param name="autoSave"></param>
		public void DoAnalysis(bool autoSave)
		{
			if (autoSave)
			{
				DoAnalysis();
				SaveAnalysisResult();
			}
		}

		/// <summary>
		/// Load the analysis result from file if it exists, or
		/// perform the analysis from scratch if not.
		/// 
		/// Will automatically save the result data if the analysis is
		/// required.
		/// </summary>
		public void LoadOrDoAnalysis()
		{
			if (!AnalysisResultSaved)
			{
				DoAnalysis(true);
			}
			else
			{
				LoadAnalysisResult();
			}
		}

		/// <summary>
		/// Save the analysis result to a file
		/// </summary>
		abstract public void SaveAnalysisResult();

		/// <summary>
		/// Load the analysis result from a file
		/// </summary>
		abstract public void LoadAnalysisResult();
	}
}
